﻿Public Class xtf_ABMalteracion

    Dim ulg_row As DataRow
    Dim obj_Alteracion As cls_alteracion_ = Nothing

    Public Sub New()
        InitializeComponent()
        AgregarValidaciones()
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            obj_Alteracion = New cls_Alteracion_
            btn_Registro.Enabled = False
            cbo_tipo.SelectedIndex = 0
            Me.Show()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef Alteracion As cls_alteracion_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_Alteracion) Then
            obj_Alteracion = New cls_Alteracion_
        End If
        obj_Alteracion = Alteracion
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_Alteracion.ToString
        txt_ID.Text = obj_Alteracion.idAlteracion
        txt_Nombre.Text = obj_Alteracion.nombre
        txt_Descripcion.Text = obj_Alteracion.descripcion
        txt_formula.Text = obj_Alteracion.formula
        cbo_tipo.SelectedIndex = obj_Alteracion.tipo
        btn_Registro.Enabled = True

        Me.Show()

    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_Alteracion.nombre = txt_Nombre.Text
            obj_Alteracion.descripcion = txt_Descripcion.Text
            obj_Alteracion.tipo = cbo_tipo.SelectedIndex
            obj_Alteracion.formula = txt_formula.Text
            If txt_ID.Text = "" Then
                obj_Alteracion.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_LISTADOAlteracion_).AgregarItem(obj_Alteracion)
                End If
                Modificar(obj_Alteracion, frm_listado, ulg_row)
            Else
                obj_Alteracion.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_LISTADOAlteracion_).ModificarItem(obj_Alteracion, ulg_row)
                End If
                Modificar(obj_Alteracion, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_Alteracion.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_Alteracion.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        If ComprobarDatos() Then
                            btn_Guardar_Click(Nothing, Nothing)
                        Else
                            Exit Sub
                        End If
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

    Private Sub cbo_tipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_tipo.SelectedIndexChanged
        If cbo_tipo.SelectedIndex = 2 Then
            lbl_Formula.Text = "Formula"
            txt_formula.Properties.Buttons(0).Visible = True
            txt_formula.Properties.ReadOnly = True
            txt_formula.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.None
            txt_formula.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None
            txt_formula.Properties.Mask.EditMask = ""
            txt_formula.Properties.Mask.UseMaskAsDisplayFormat = False
            If Not IsNumeric(obj_Alteracion.formula) Then
                txt_formula.Text = obj_Alteracion.formula
            Else
                txt_formula.Text = ""
            End If
        Else
            lbl_Formula.Text = "Valor"
            txt_formula.Properties.Buttons(0).Visible = False
            txt_formula.Properties.ReadOnly = False
            txt_formula.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            txt_formula.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
            txt_formula.Properties.Mask.EditMask = "n" & int_cantdecimales
            txt_formula.Properties.Mask.UseMaskAsDisplayFormat = True
            If IsNumeric(obj_Alteracion.formula) Then
                txt_formula.Text = obj_Alteracion.formula
            Else
                txt_formula.Text = ""
            End If
        End If
    End Sub

    Private Sub txt_formula_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_formula.ButtonClick
        Dim ABMFormula As New xtf_ABMFormula(txt_formula.Text)
        If ABMFormula.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txt_formula.Text = ABMFormula.txt_formula.Text
        End If
    End Sub
End Class